package com.zaofan.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.zaofan.bean.DynamicReply;
import com.zaofan.bean.DynamicReplyDTO;
import com.zaofan.constant.UrlConstant;
import com.zaofan.dao.base.DaoSupportImpl;

public class DynamicReplyDao extends DaoSupportImpl<DynamicReply> {

	public List<DynamicReplyDTO> findByDynamicIdWithOther(String begin, String end, String dynamicId, String replyType,
			String state) {
		try {
			String sql = "select dynamicreply.*,account.nickName as userName,account.mobile as userMobile, concat('"
					+ UrlConstant.HOST_URL + "',account.photo) as userPhoto from dynamicreply "
					+ "left join account on dynamicreply.userId = account.id " + "where dynamicreply.replyId = "
					+ dynamicId;

			if (replyType != null) {
				sql += " and dynamicreply.replyType = " + replyType;
			}
			if (state != null) {
				sql += " and dynamicreply.state = " + state;
			}
			sql += " order by dynamicreply.addTime desc limit " + begin + "," + end + ";";
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new BeanListHandler<>(DynamicReplyDTO.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Long countByDynamicIdWithOther(String dynamicId, String replyType, String state) {
		try {
			String sql = "select count(*) from dynamicreply " + "where replyId = " + dynamicId;
			if (replyType != null) {
				sql += " and dynamicreply.replyType = " + replyType;
			}
			if (state != null) {
				sql += " and dynamicreply.state = " + state;
			}
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new ScalarHandler<Long>());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}

	public List<DynamicReplyDTO> findByUserIdWithOther(String begin, String end, String userId, String replyType,
			String state) {
		try {
			String update = "";
			String sql = "select dynamicreply.*,account.nickName as userName,account.mobile as userMobile, concat('"
					+ UrlConstant.HOST_URL + "',account.photo) as userPhoto from dynamicreply "
					+ "left join account on dynamicreply.userId = account.id " + "where dynamicreply.authorId = "
					+ userId + " and dynamicreply.userId != " + userId;

			if (replyType != null) {
				sql += " and dynamicreply.replyType = " + replyType;
			}
			if (state != null) {
				sql += " and dynamicreply.state = " + state;
				update = "update dynamicreply set state = 1 where state = 0 and  authorId = " + userId
						+ " and userId != " + userId;
				if (replyType != null) {
					update += " and replyType = " + replyType;
				}
				update += " order by addTime limit " + end + ";";
			}
			sql += " order by dynamicreply.addTime desc limit " + begin + "," + end + ";";
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			List<DynamicReplyDTO> datas =  runner.query(sql, new BeanListHandler<>(DynamicReplyDTO.class));
			if(!"".equals(update)){
				System.err.println(update);
				runner.update(update);
			}
			return datas;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Long countUnRead(Integer userId, Integer replyType) {
		try {
			String sql = "select count(*) from dynamicreply where state = 0 and authorId = " + userId
					+ " and userId != " + userId + " and replyType = " + replyType;
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new ScalarHandler<Long>());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}

	// public List<DynamicReplyDTO> findByDynamicIdWithOther(int id, Integer
	// replyType) {
	// try {
	// String sql = "select dynamicreply.*,account.nickName as
	// userName,account.mobile as userMobile from dynamicreply "
	// + "left join account on dynamicreply.userId = account.id " + "where
	// dynamicreply.replyId = " + id;
	//
	// if (replyType != null) {
	// sql += " and dynamicreply.replyType = " + replyType;
	// }
	// sql += " order by dynamicreply.type desc, dynamicreply.addTime desc";
	// System.err.println(sql);
	// QueryRunner runner = new QueryRunner(getDataSource());
	// return runner.query(sql, new BeanListHandler<>(DynamicReplyDTO.class));
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }
	// return null;
	// }

}
